<?php
/**
 * SessionMemcache.class.php    session memcache存储类
 *
 * @author                      mashanling(msl-138@163.com)
 * @date                        2012-09-19 16:52:43
 * @last modify                 2012-09-19 21:22:44 by mashanling
 */
class SessionMemcache {
    private $lifetime;
    private $memcache;

    /**
     * 构造函数
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:05:50
     * @last modify  2012-09-19 21:22:56 by mashanling
     *
     * @return void 无返回值
     */
    function __construct() {

        if (!extension_loaded('memcache')) {
            throw new Exception(L('_NOT_SUPPERT_') . ':memcache');
        }

        $this->memcache = new Memcache();

        if (!$this->memcache->connect(sys_config('sys_memcache_host'), sys_config('sys_memcache_port'))) {
            throw new Exception(L('CAN_NOT,CONNECT') . 'Memcache' . L('SERVER'));
        }
    }

    /**
     * session_set_save_handler  close方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:06:08
     * @last modify  2012-09-19 17:06:08 by mashanling
     *
     * @return bool
     */
    function close() {
        return true;
    }

    /**
     * 删除指定的session_id
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:06:20
     * @last modify  2012-09-19 17:06:20 by mashanling
     *
     * @param string $session_id session_id
     *
     * @return bool
     */
    function destroy($session_id) {
        return $this->memcache->delete($session_id);
    }

    /**
     * 入口方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:20:31
     * @last modify  2012-09-19 17:20:31 by mashanling
     *
     * @return void 无返回值
     */
    function execute() {
        $this->lifetime = sys_config('sys_session_gc_maxlifetime');
        session_set_save_handler(array(&$this, 'open'), array(&$this, 'close'), array(&$this, 'read'), array(&$this, 'write'), array(&$this, 'destroy'), array(&$this, 'gc'));
    }

    /**
     * 删除过期的 session
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:06:55
     * @last modify  2012-09-19 17:06:55 by mashanling
     *
     * @param $maxlifetime 存活期时间
     *
     * @return bool
     */
    function gc($maxlifetime) {
        return true;
    }

    /**
     * session_set_save_handler  open方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:07:31
     * @last modify  2012-09-19 17:07:31 by mashanling
     *
     * @param $save_path
     * @param $session_name
     *
     * @return true
     */
    function open($save_path, $session_name) {
        return true;
    }

    /**
     * 读取session_id
     * session_set_save_handler  read方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:07:43
     * @last modify  2012-09-19 17:07:43 by mashanling
     *
     * @param string $session_id session_id
     *
     * @return string session值
     */
    function read($session_id) {
        return $this->memcache->get($session_id);
    }

    /**
     * 写入session_id 的值
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-19 17:10:04
     * @last modify  2012-09-19 17:10:04 by mashanling
     *
     * @param string $session_id session_id
     * @param string $data       session数据
     *
     * @return bool
     */
    function write($session_id, $data) {
        $method = $this->read($session_id) === false ? 'add' : 'replace';

        return $this->memcache->$method($session_id, $data, false, $this->lifetime);
    }
}